package com.lanzchina.lanclass.service;

import com.lanzchina.lanclass.constant.Constant;
import com.lanzchina.lanclass.domain.Menu;
import com.lanzchina.lanclass.domain.Result;
import com.lanzchina.lanclass.domain.School;
import com.lanzchina.lanclass.repository.SchoolRepository;
import com.lanzchina.lanclass.utils.Md5Util;
import com.lanzchina.lanclass.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SchoolService {

    @Autowired
    private SchoolRepository schoolRepository;

    /**
     * 当前openId未绑定用户账号
     * @param validateCode 验证码
     * @return 返回所有学校
     */
    public Result findAllSchool(String validateCode) {
        if (!Md5Util.md5(Constant.SIGN_KEY).equals(validateCode)) {
            return ResultUtil.error(0, "验证失败");
        }
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> schoolMapList = new ArrayList<>();
        for (School school: schoolRepository.findAll()) {
            Map<String, Object> schoolMap = new HashMap<>();
            schoolMap.put("schoolCode", school.getSchoolCode());
            schoolMap.put("schoolName", school.getSchoolName());
            schoolMapList.add(schoolMap);
        }
        data.put("schoolList", schoolMapList);
        return ResultUtil.success(data);
    }

    /**
     * 根据schoolCode查找指定学校
     * @param schoolCode 学校代码
     * @param validateCode 验证码
     * @return 返回选中学校所有信息
     */
    public Result findSchool(Integer schoolCode, String validateCode) {
        String myValidateCode = Md5Util.validateCode("schoolCode", schoolCode.toString());
        if (!myValidateCode.equals(validateCode)) {
            return ResultUtil.error(0, "验证失败");
        }
        School school = schoolRepository.findBySchoolCode(schoolCode);
        if (school == null) {
            return ResultUtil.error(0, "学校码有误");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("schoolInfo", formatSchoolInfo(school));
        return ResultUtil.success(data);
    }

    /**
     * 格式化学校信息
     * @param school 学校对象
     * @return 学校
     */
    public Map<String, Object> formatSchoolInfo(School school) {
        Map<String, Object> schoolInfo = new HashMap<>();
        schoolInfo.put("schoolName", school.getSchoolName());
        schoolInfo.put("schoolCode", school.getSchoolCode());
        schoolInfo.put("serverAddress", school.getServerAddress());
        schoolInfo.put("attendanceAddress", school.getAttendanceAddress());
        schoolInfo.put("barrageIP", school.getBarrageIP());
        schoolInfo.put("barragePort", school.getBarragePort());
        schoolInfo.put("barrageUrl", school.getBarrageUrl());
        schoolInfo.put("mqPassword", school.getMqPassword());
        schoolInfo.put("mqUser", school.getMqUser());
        schoolInfo.put("workAddress", school.getWorkAddress());
        schoolInfo.put("menuList", formatMenusInfo(school.getMenus(), school));
        return  schoolInfo;
    }

    private Map<String, Object> formatMenu(Menu menu) {
        Map<String, Object> menuInfo = new HashMap<>();
        menuInfo.put("menuName", menu.getMenuName());
        menuInfo.put("vcTitle", menu.getVcTitle());
        return menuInfo;
    }

    private List<Map<String, Object>> formatMenusInfo(List<Menu> menus, School school) {
        List<Map<String, Object>> menuMapList = new ArrayList<>();
        for (Menu menu: menus) {
            if (menu.getGrade() == 1) {
                Map<String, Object> menuMap = formatMenu(menu);
                List<Menu> childMenus = school.filterMenu(menu.getMenuId());
                List<Map<String, Object>> childMenuMapList = new ArrayList<>();
                for (Menu secondMenu: childMenus) {
                    childMenuMapList.add(formatMenu(secondMenu));
                }
                menuMap.put("childMenuList", childMenuMapList);
                menuMapList.add(menuMap);
            }
        }
        return menuMapList;
    }
}
